package com.whf.Controller;

import com.whf.VO.ProductInfoVO;
import com.whf.VO.ProductVO;
import com.whf.VO.ResultVO;
import com.whf.dataobject.ProductCategory;
import com.whf.dataobject.ProductInfo;
import com.whf.service.CategoryService;
import com.whf.service.ProductService;
import com.whf.utils.ResultVOUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@RestController
@RequestMapping("/buyer/product")
public class BuyerProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private CategoryService categoryService;
    @GetMapping("/list")
    @Cacheable(cacheNames = "product",key ="123" )
    //如果获取的是不同id的list时list(id),
    //可以@Cacheable(cacheNames = "product",key ="#id" )来分类存储
    //还有判断属性condition="#id.length()>3" 判断id长度是否大于3,大于3就缓存
    //根据解决来判断是否缓存 unless="#result.getcode!=0",结果正确的时候缓存
    public ResultVO list(){
        //1.查询所有的上架商品
        List<ProductInfo> productInfoList = productService.findUpAll();
        //2.查询所有目录(一次性查询所有)
       /* List<Integer> categoryTypeList=new ArrayList();
        //传统方法:
        for(ProductInfo productInfo:productInfoList){
            categoryTypeList.add(productInfo.getCategoryType());
        }*/
        //精简方法(java8,lambda)
        List<Integer> categoryTypeList=productInfoList.stream().map(e-> e.getCategoryType()).collect(Collectors.toList());
        List<ProductCategory> categoryList = categoryService.findByCategoryTypeIn(categoryTypeList);
        //3.拼装信息
        List<ProductVO> productVOList=new ArrayList<>();
        //遍历分类信息
        for(ProductCategory productCategory:categoryList){
            ProductVO productVO=new ProductVO();
            productVO.setCategoryName(productCategory.getCategoryName());
            productVO.setCategoryType(productCategory.getCategoryType());
            List<ProductInfoVO> productInfoVOList=new ArrayList<>();
        //遍历详情
            for(ProductInfo productInfo:productInfoList){
                //根据类目判断是否属于该类
                if(productInfo.getCategoryType().equals(productCategory.getCategoryType())){
                    ProductInfoVO productInfoVO=new ProductInfoVO();
                    //spring提供了beanutils的该方法可以把一个对象的值,cope到另一个对象中,前者时数据源
                    BeanUtils.copyProperties(productInfo,productInfoVO);
                    productInfoVOList.add(productInfoVO);
                }
            }
            productVO.setProductInfoVOList(productInfoVOList);
            productVOList.add(productVO);
        }

        return ResultVOUtil.success(productVOList);
    }
}
